﻿using Leo.Awesome.Attachment.Config;
using Leo.Awesome.Attachment.Localization;
using Localization.Resources.AbpUi;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using Volo.Abp.BlobStoring;
using Volo.Abp.BlobStoring.Minio;
using Volo.Abp.Localization;
using Volo.Abp.Modularity;

namespace Leo.Awesome.Attachment;

[DependsOn(
    typeof(AttachmentApplicationContractsModule),
    typeof(AbpBlobStoringMinioModule)
    )]
public class AttachmentHttpApiModule : AbpModule
{

    private string _endPoint;
    private string _accessKey;
    private string _secretKey;
    private readonly string containerName = "leo-minio-container-files";
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        ConfigureLocalization();
        ConfigureMinio(context);
    }
    /// <summary>
    /// Use Minio as the system storage solution
    /// </summary>
    /// <param name="context"></param>
    private void ConfigureMinio(ServiceConfigurationContext context)
    {
        var configuration = context.Services.GetConfiguration();
        Configure<MinioOption>(configuration.GetSection("Minio"));
        _endPoint = configuration["Minio:EndPoint"];
        _accessKey = configuration["Minio:AccessKey"];
        _secretKey = configuration["Minio:SecretKey"];
        Configure<AbpBlobStoringOptions>(options =>
        {
            options.Containers.ConfigureDefault(container =>
            {
                container.UseMinio(minio =>
                {
                    minio.EndPoint = _endPoint;
                    minio.AccessKey = _accessKey;
                    minio.SecretKey = _secretKey;
                    minio.WithSSL = false;
                    minio.BucketName = containerName;
                    minio.CreateBucketIfNotExists = true;
                });
            });
        });
    }

    private void ConfigureLocalization()
    {
        Configure<AbpLocalizationOptions>(options =>
        {
            options.Resources
                .Get<AttachmentResource>()
                .AddBaseTypes(
                    typeof(AbpUiResource)
                );
        });
    }
}
